<?php
/**
 * Create by 11li
 * Date 2020/1/8
 * Time 11:09
 * 测试
 */

namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\PaymentRecord;
use app\common\model\Promoter;
use app\common\service\OrderService;
use think\Db;

class Test extends Api
{
	protected $noNeedLogin = '*';
	
	
	public function index()
	{
		$model = new Promoter();
		$sendCommission = 5;
		$promoterRes = Db::name('promoter')
			->where('user_id', 13)
			->inc('commission', $sendCommission)
			->dec('disabled_commission', $sendCommission)
			->update(['update_time' => time()]);
		
		echo Db::name('promoter')->getLastSql();
	}
	
	/**
	 * 根据用户ID直接登录（测试使用)
	 */
	public function loginById($id) {
		$this->auth->direct($id);
		$userInfo = $this->auth->getUserinfo();
		$this->success('登录成功', $userInfo);
	}
	
	/**
	 * 切换用户
	 */
	public function changeUser() {
		$openId = 'oVw4L451XsGKE6SAPVCep2zJnb40';
		$id = $this->request->get('id');
		Db::name('user')
			->where(['smallapp_openid' => $openId])
			->update(['smallapp_openid' => $openId.'_'.time()]);
		
		Db::name('user')
			->where(['id' => $id])
			->update(['smallapp_openid' => $openId]);
		
		$this->auth->direct($id);
		
		$userInfo = $this->auth->getUserinfo();
		$this->success('登录成功', $userInfo);
	}
	
	/**
	 * 支付成功
	 */
	public function orderPaySuccess($id) {
		$row = PaymentRecord::get($id);
		$obj = new OrderService();
		$res = $obj->paySuccess($id, $row['amount'], time());
		
		$this->success($obj->getError(), $res);
	}
	
}